package com.kzd.btgame.main

import android.graphics.Typeface
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.*
import com.google.android.material.tabs.TabLayout
import com.kzd.btgame.R
import com.kzd.btgame.app.AppFlowBus
import com.kzd.btgame.base.BaseViewModel
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.databinding.FragmentMainHomeBinding
import com.kzd.btgame.entity.ReserveChangeResponse
import com.kzd.btgame.jetpack.vm.HomeViewModel
import com.kzd.btgame.main.home.HomeFeaturedFragment
import com.kzd.btgame.main.home.HomeRankingFragment
import com.kzd.btgame.main.home.HomeTravelFragment
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel

class MainHomeFragment : OwnerViewBindingFragment<FragmentMainHomeBinding>() {

    companion object {
        fun newInstance(): MainHomeFragment {
            return MainHomeFragment()
        }
    }

    private val mTabs = mutableListOf("新游", "推荐", "排行榜")


    val mViewModel: HomeViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> FragmentMainHomeBinding
        get() = FragmentMainHomeBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.includeAppSearch2.root
            barColor = R.color.white
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

    }

    override fun initData() {

        mViewBinding.tabMainHomeKind.addOnTabSelectedListener(object :
            TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) = Unit

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                tab?.customView = null
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                val text: TextView = TextView(context).apply {
                    setTextColor(ColorUtils.getColor(R.color.black))
                    setTypeface(null, Typeface.BOLD)
                    textSize = 18f
                    gravity = Gravity.CENTER
                }
                text.text = tab?.text
                tab?.customView = text
            }
        })

        val mFragments = mutableListOf(
            HomeTravelFragment.newInstance(),
            HomeFeaturedFragment.newInstance(),
            HomeRankingFragment.newInstance()
        )
        mViewBinding.vpMainHomeBody.apply {
            setNoScroll(false)
        }.adapter = object : FragmentStatePagerAdapter(
            childFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        ) {

            override fun getItem(position: Int): Fragment {
                return mFragments[position]
            }

            override fun getCount(): Int = mTabs.size

            override fun getPageTitle(position: Int): CharSequence {
                return mTabs[position]
            }

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {}
        }

        mViewBinding.tabMainHomeKind.apply {
            setupWithViewPager(mViewBinding.vpMainHomeBody)
        }.getTabAt((1))?.select()

        //进入搜索
        mViewBinding.includeAppSearch2.tvAppSearchHint.setOnClickAntiShakeListener {
            toHomeSearchActivity()
        }
        //下载管理
        mViewBinding.includeAppSearch2.tvAppSearchDown.setOnClickAntiShakeListener {
            toMineDownloadActivity()
        }
        //消息中心
        mViewBinding.includeAppSearch2.tvAppSearchMsg.setOnClickAntiShakeListener {
            toMineMessageActivity()
        }
    }


    override fun initViewMode() {

        BaseViewModel.userAppTipsLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mViewBinding.includeAppSearch2.viewAppSearchMsgRead.visibility =
                if (data.msg_rs != 1) View.GONE else View.VISIBLE
        })


        AppFlowBus.observe(viewLifecycleOwner, ReserveChangeResponse::class.java,
            androidx.lifecycle.Observer<ReserveChangeResponse> {
                val gid = it.data.id
                    ?: return@Observer
                val type = if (it.data.is_yuyue)
                    "1" else "0"
                checkLogin {
                    mViewModel.reserveChange(gid, type, it.position)
                }
            })
    }
}